package com.interview.javabase.thread;

import java.util.concurrent.ExecutionException;

/**
 * @author: zsg
 * @description: 验证JMM内存模型的可见性
 * @date: 2019/6/29 10:25
 * @modified:
 */
public class Demo5 {
    public static void main(String[] args) {

        Test1 test1 = new Test1();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " 当前的值为 ：" + test1.getNumber());

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 改变number的值
            test1.changeNumber();

            System.out.println(Thread.currentThread().getName() + " 当前的值为 ：" + test1.getNumber());

        }, "AA").start();

        while (test1.getNumber() == 0) {
        }
        System.out.println(Thread.currentThread().getName() + " 当前的值为 ：" + test1.getNumber());
    }
}

class Test1 {
    private volatile int number = 0;

    public void changeNumber() {
        this.number = 100;
    }

    public int getNumber() {
        return number;
    }

    public Test1 setNumber(int number) {
        this.number = number;
        return this;
    }
}